package com.longdaji.decoration.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.longdaji.decoration.R;
import com.longdaji.decoration.bean.RefundGoods;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Kanade on 2018/5/24 0024.
 */

public class RefundAdapter extends RecyclerView.Adapter<RefundAdapter.ViewHolder> {
    private List<RefundGoods> mRefundGoodsList;
    private Context mContext;

    public RefundAdapter(List<RefundGoods> refundGoods) {
        this.mRefundGoodsList = refundGoods;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_commodity_name)
        TextView tvCommodityName;
        @BindView(R.id.tv_commodity_attr)
        TextView tvCommodityAttr;
        @BindView(R.id.tv_commodity_num)
        TextView tvCommodityNum;
        @BindView(R.id.iv_commodity_picture)
        ImageView ivCommodityPicture;
        @BindView(R.id.tv_commodity_price)
        TextView tvCommodityPrice;
        @BindView(R.id.btn_see_details)
        Button btnSeeDetails;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick({R.id.btn_see_details})
        void onItemViewClicked(View view) {
            switch (view.getId()) {
                case R.id.btn_see_details:
                    mRefundGoodsListener.onSeeDetailsClick(getAdapterPosition());
                    break;
                default:
                    break;
            }
        }

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_refund_goods, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        RefundGoods refundGoods = mRefundGoodsList.get(position);
        holder.tvCommodityName.setText(refundGoods.getGoodsName());
        holder.tvCommodityAttr.setText(refundGoods.getGoodsAttr());
        holder.tvCommodityNum.setText("x" + refundGoods.getGoodsSkuNum());
        holder.tvCommodityPrice.setText("￥" + refundGoods.getGoodsPrice());
        Glide.with(mContext).load(refundGoods.getGoodsSkuImg()).into(holder.ivCommodityPicture);
    }


    @Override
    public int getItemCount() {
        return mRefundGoodsList.size();
    }

    public void setData(List<RefundGoods> list) {
        mRefundGoodsList.clear();
        mRefundGoodsList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearData() {
        mRefundGoodsList.clear();
        notifyDataSetChanged();
    }


    public interface RefundGoodsListener {
        void onSeeDetailsClick(int position);
    }

    private RefundGoodsListener mRefundGoodsListener;

    public void setRefundGoodsListener(RefundGoodsListener listener) {
        this.mRefundGoodsListener = listener;
    }


}
